package com.my.hp_hr.dao;

import com.my.hp_hr.entity.Department;
import com.my.hp_hr.entity.Employee;
import com.my.hp_hr.entity.History;
import com.my.hp_hr.entity.Position;
import com.my.hp_hr.util.BaseDAO;

import java.util.Date;
import java.util.List;


public class HistoryDao extends BaseDAO {
    public List<Employee> getWorkServlet(String pageNo) throws Exception {
        String sql = "select h.employee_number,h.name,h.gender,h.telephone,p.name positionName,d.name departmentName,h.birthday,h.email,h.education,h.notes,h.in_time,h.address,h.password,h.photo,h.id,h.department_number,h.position_number from employee h\n" +
                "                    inner join position p on h.position_number = p.position_number\n" +
                "                    inner join department d on h.department_number = d.department_number limit ?,?";
        int page = (Integer.parseInt(pageNo)-1)*4;
        Object[] obj = {page,4};
        List<Employee> list = getList2(sql, new Employee(), obj);
        return list;
    }

    public int getTotal() throws Exception {
        String sql = "select count(*) from employee h\n" +
                "    inner join position p on h.position_number = p.position_number\n" +
                "    inner join department d on h.department_number = d.department_number\n" +
                "    ";

        int count = getCount(sql);
        return count;
    }

    public List<Position> getPosition() throws Exception {
        String sql ="select * from position";
        List<Position> list = getList(sql, new Position());
        return list;
    }

    public List<Department> getDepartment() throws Exception {
        String sql = "select * from department";
        List<Department> list = getList(sql, new Department());
        return list;
    }

    public int getAllHistory() throws Exception {
        String sql = "select max(employee_number) from employee";
        //History his = getOne(sql, new History());
        int count = getCount(sql);
        return count;
    }

    public void addHistory(String employeeNumber, String name, String password, String gender, String date, String telephone, String email, String address, String education, String departmentNumber, String positionNumber, String notes) throws Exception {
//        Date date1 = new Date();
//        String sql ="insert into history values (null,?,?,?,?,?,?,?,null,?,?,null,?,?,?,null,?)";
//        Object[] obj = {employeeNumber,name,gender,date,telephone,email,address,education,date1,departmentNumber,positionNumber,status,notes};
        Date date1 = new Date();
        String sql ="insert into employee values(null,?,?,?,?,?,?,?,null,?,?,?,?,?,?)";
        Object[] obj ={employeeNumber,name,gender,date,telephone,email,address,education,departmentNumber,positionNumber,date1,password,notes};
        addOrupdOrdel(sql,obj);
    }

    public Employee getEmployeeMsg(String id) throws Exception {
        String sql = "select h.employee_number,h.name,h.gender,h.telephone,p.name positionName,d.name departmentName,h.birthday,h.email,h.education,h.notes,h.in_time,h.address,h.password,h.photo,h.id,h.position_number,h.department_number from employee h\n" +
                "                    inner join position p on h.position_number = p.position_number\n" +
                "                    inner join department d on h.department_number = d.department_number where h.id =?;";
        Object[] obj = {id};
        Employee employee = getOne2(sql, new Employee(), obj);
        return employee;
    }

    public void updateEmployee(String employeeNumber, String name, String password, String gender, String date, String telephone, String email, String address, String education, String departmentNumber, String positionNumber, String notes) throws Exception {
        String sql ="update employee set name=?,gender=?,birthday=?,telephone=?,email=?,address=?,education=?,department_number=?,position_number=?,password=?,notes=? where employee_number=?";
        Object[] obj ={name,gender,date,telephone,email,address,education,departmentNumber,positionNumber,password,notes,employeeNumber};
        addOrupdOrdel(sql,obj);
    }

    public void deleteEmployee(String id) throws Exception {
        String sql = "delete from employee where id =?";
        Object[] obj = {id};
        addOrupdOrdel(sql,obj);
    }
}
